package com.happy016.controller;

import com.happy016.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/swagger")
public class SwaggerController {

    List<User> userList = new ArrayList<>();

    @ApiOperation(value = "获取user列表数据")
    @RequestMapping(value = "",method = RequestMethod.GET)
    public List<User> getUserList(){
        if(userList.isEmpty()){
            User user1 = new User(1,"username_1",1);
            User user2 = new User(2,"username_2",2);
            User user3 = new User(3,"username_3",3);
            userList.add(user1);
            userList.add(user2);
            userList.add(user3);
        }
        return userList;
    }

    @ApiOperation(value = "获取单个user数据",notes = "根据Url的id来获取对应的数据")
    @ApiImplicitParam(name = "id", value = "user的id", required = true, dataType = "Integer",paramType = "path")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User getUser(@PathVariable Integer id){
        return new User(id,"username_"+id,id);
    }

    @ApiOperation(value="新增user", notes="根据User对象新增")
    @ApiImplicitParam(name = "user", value = "详细实体user", required = true, dataType = "User")
    @RequestMapping(value="", method=RequestMethod.POST)
    public String addUser(User user) {
        userList.add(user);
        return "success";
    }

    @ApiOperation(value="更新用户信息", notes="更新指定id的用户名为指定的username")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "user的id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "username",paramType ="form", value = "username", required = true, dataType = "String")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String updateUser(@PathVariable Integer id, String username) {
        if (!userList.isEmpty()){
            for (int i = userList.size() - 1; i>= 0; i--){
                User user = userList.get(i);
                if (id.equals(user.getId())){
                    user.setName(username);
                }
            }
        }
        return "success";
    }

    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", paramType = "path",value = "用户ID", required = true, dataType = "Integer")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id) {
        if (!userList.isEmpty()){
            for (int i = userList.size() - 1; i>= 0; i--){
                User user = userList.get(i);
                if (id.equals(user.getId())){
                    userList.remove(i);
                }
            }
        }
        return "success";
    }
}
